layui.use(["form","laypage","layer","table"],function(){
	var form=layui.form;
	var laypage=layui.laypage;
	var layer=layui.layer;
	var table=layui.table;
	
	//当前页
	var pageCurr=1;
	$(function () {
		initData("","");
		/* 全选/全不选 */
		$("#check_all").click(function () {
			var flag=$(this).prop("checked");
			$(".check_item").prop("checked",flag);
		});
		$(document).on("click",$(".check_item"),function(){
			if($(".check_item:checked").length==$(".check_item").length){
				$("#check_all").prop("checked",true);
			}else{
				$("#check_all").prop("checked",false);
			};
		});
		
		/* 批量删除员工 */
		$("#deleteAllEmployeeBtn").click(function () {
			if($(".check_item:checked").length>0){
				var delNameStr="[";
				var delIdStr="";
				$.each($(".check_item:checked"),function(){
					delNameStr+=$(this).parents("tr").find("td").eq(2).text()+",";
					delIdStr+=$(this).parents("tr").find("td").eq(1).text()+"-";
				});
				delNameStr=delNameStr.substring(0, delNameStr.length-1)+"]";
				delIdStr=delIdStr.substring(0, delIdStr.length-1);
				layer.alert("确定要删除"+delNameStr+"吗？",{btn:["确定","取消"]},function(){
					delEmployee(delIdStr);
				});
			}else{
				layer.alert("请先勾选需要删除的员工");
			}
			
		});
		
		/* 删除单个员工 */
		$(document).on("click",".delEmployeeBtn",function(){
			var empName=$(this).parents("tr").find("td:eq(2)").text();
			var empId=$(this).attr("emp-id");
			layer.alert("确定删除"+empName+"吗？",{btn:["确定","取消"]},function(){
				delEmployee(empId);
			});
		});
		
		/* 执行删除员工 */
		function delEmployee(empIds) {
			var url="employee/"+empIds;
			$.ajax({
				url:url,
				type:"DELETE",
				success:function(result){
					if(result.success){
						layer.closeAll();
						initData(pageCurr, "");
					}else{
						layer.alert(result.msg);
					}
				}
			});
		}
		
		/* 点击添加按钮弹出添加员工界面 */
		$("#addEmplpyeeBtn").click(function () {
			show_form("添加员工",null,null);
		});
		
	});
	
	
	/* 弹出表单界面 */
	function show_form(titleStr,updateId,updateEmpName) {
		/* 弹出表单界面 */
		layer.open({
			type:1,
			title:titleStr,
			skin:"myclass",
			area:["50%"],
			content:$("#test").html()
		});
		
		/* 异步获取部门数据填充到部门Select */
		var url="departments";
		$.get(url,function(result){
			var departments=result.data.departments;
			var deptSelect=$(".deptSelect").eq(1);
			deptSelect.html("");
			$.each(departments,function(index,data){
				var deptOption=$("<option value='"+data.deptId+"'>"+data.deptName+"</option>");
				deptSelect.append(deptOption);
			});
			if(updateId!=null){
				var url="employee/"+updateId;
				$.get(url,function(result){
					var employee=result.data.employee;
					$.each($("input:radio"),function(){
						if($(this).val()==employee.empSex){
							$(this).prop("checked",true);
						}else{
							$(this).prop("checked",false);
						}
					});
					$(".empName").val(employee.empName);
					$(".empEmail").val(employee.empEmail);
					$(".deptSelect").val(employee.deptId);
					form.render();
				});	
			}else{
				form.render();
			}
		});
		
		/* 表单验证 */
		form.verify({
			check_user:function(value, item){
				if(!new RegExp("^[\u4e00-\u9fa5]{2,5}$").test(value)){
					return "姓名必须是2-5位的中文";
				}
			}
		});
		
		$(".empName").eq(1).blur(function () {
			$(".addEmployee").eq(1).attr("disabled",false).removeClass("layui-btn-disabled");
			if(updateId==null||(updateId!=null&&updateEmpName!=$(this).val())){
				var url="checkUser";
				$.post(url,"employeeName="+$(".empName").eq(1).val(),function(result){
					if(result.success){
						$(".addEmployee").eq(1).attr("disabled",false).removeClass("layui-btn-disabled");
					}else{
						layer.tips('该员工已存在', $(".empName").eq(1) , {
							tips: [1]
						});
						$(".addEmployee").eq(1).attr("disabled",true).addClass("layui-btn-disabled");
					}
				}); 
			}
		});
		
		form.on('submit(go)', function(data){
			var url="employee/"+updateId;
			if(updateId!=null){
				$.ajax({
					url:url,
					type:"PUT",
					data:data.field,
					success:function(result){
						if(result.success){
							layer.closeAll();
							initData(pageCurr, "");
						}else{
							$.each(result.data.errorFieldsMap,function(key){
								show_validator_error($("."+key).eq(1), result.data.errorFieldsMap[key]);
							});
						}
					}
				});
			}else{
				var url="employee";
				$.post(url,data.field,function(result){
					if(result.success){
						layer.closeAll();
						initData(9999, "");
					}else{
						$.each(result.data.errorFieldsMap,function(key){
							show_validator_error($("."+key).eq(1), result.data.errorFieldsMap[key]);
						});
					}
				});
			  return false;
			}
		});
		
	}
	
	/* 显示错误提示 */
	function show_validator_error(ele,msg) {
		layer.tips(msg, ele , {tips: [1], tipsMore:true});
	}
	
	/* 初始化分页数据 */
	function initData(pageNum,pageSize) {
		layer.closeAll();
		$(".check_item").prop("checked",false);
		var url="employee?pageNum="+pageNum+"&pageSize="+pageSize;
		$.get(url,function(result){
			var pageInfo=result.data.pageInfo;
			var employees=pageInfo.list;
			var employeeTr;
			
			/*填充表格数据*/
			$("#table tbody").html("");
			$.each(employees,function(index,item){
				employeeTr=$("<tr>"
						+"<td><input type='checkbox' class='check_item'></td>"
						+"<td>"+item.empId+"</td>"
						+"<td>"+item.empName+"</td>"
						+"<td>"+item.empSex+"</td>"
						+"<td>"+item.empEmail+"</td>"
						+"<td>"+item.department.deptName+"</td>"
						+"<td>"
							+"<button emp-id='"+item.empId+"' class='layui-btn layui-btn-small layui-btn-normal updateEmployeeBtn'><i class='layui-icon'>&#xe642;</i>编辑</button>"
							+"<button emp-id='"+item.empId+"' class='layui-btn layui-btn-small layui-btn-danger delEmployeeBtn'><i class='layui-icon'>&#xe640;</i>删除</button>"
						+"</td>"
					+"</tr>");
				employeeTr.appendTo("#table tbody");
			});
			
			/* 点击编辑按钮弹出表单界面并回显数据 */
			$(".updateEmployeeBtn").click(function () {
				var empId=$(this).parents("tr").find("td:eq(1)").text();
				var empName=$(this).parents("tr").find("td:eq(2)").text();
				show_form("编辑员工",empId,empName);
			});
			
			/*填充分页数据*/
			 laypage.render({
			    elem: 'paging'
			    ,curr: pageInfo.pageNum
			    ,count: pageInfo.total
			    ,limit: pageInfo.pageSize
			    ,limits:[5,10,20]
			    ,layout: ['count', 'prev', 'page', 'limit', 'next', 'skip']
			    ,jump: function(obj,first){
			    	pageCurr=obj.curr;
			      if(!first){
			    	  initData(obj.curr, obj.limit);
			      };
			    }
			  });  
		});
	}
	  
});